package com.xq.xq.kotlinlearnsingletom.kotlin

/**
 * 懒加载形式带参数的单例模式
 */
class ParamLazySingleton private constructor(c: String) {
    private var context = c

    companion object {
        @Volatile
        private var instance: ParamLazySingleton? = null

        fun getInstance(c: String): ParamLazySingleton {
            if (instance == null) {
                synchronized(ParamLazySingleton::class) {
                    if (instance == null) {
                        instance = ParamLazySingleton(c)
                    }
                }
            }
            return instance!!
        }

        fun getInstanceSimple(c: String): ParamLazySingleton =
            instance ?: synchronized(ParamLazySingleton::class) {
                instance ?: ParamLazySingleton(c).apply {
                    instance = this
                }
            }


    }

    fun eat(food: String) {
        println("ParamLazySingleton eat $food")
    }

}